package query_service

import (
	"context"
	"github.com/gin-gonic/gin"
	"mine-info-server/domain/common-context/command"
	"mine-info-server/domain/common-context/model/user-aggregate/aggregate"
	"mine-info-server/domain/common-context/repo"
	"mine-info-server/utils/consts"
	"mine-info-server/utils/redis_mq"
)

/*
   @author:zhongyang
   @date:2023/4/11
   @description:通用-命令应用服务
*/

type CommonCommandService struct {
	MsgRepo repo.MsgIRepo
}

// Login 登陆
func (s *CommonCommandService) Login(c *gin.Context, command *command.LoginCommand) (string, error) {
	// todo 查询用户聚合 这个目前还没有做用户相关的东西，所以就直接生成
	user := &aggregate.UserAggregate{}
	// 用户聚合完成领域业务操作（校验用户名密码等操作）
	token, err := user.Login(command.Username, command.Password)
	if err != nil {
		return "", err
	}
	return token, nil
}

// ReadMsg 读取一条消息
func (s *CommonCommandService) ReadMsg(c *gin.Context, msgCommand *command.ReadMsgCommand) error {
	// 查询聚合根
	msg, err := s.MsgRepo.FindMsgById(msgCommand.MsgId)
	if err != nil {
		return err
	}
	msg.ReadMsg()
	// 持久化操作
	return s.MsgRepo.ReadMsg(msg)
}

func NewCommonCommandService() *CommonCommandService {
	return &CommonCommandService{}
}

// SundayPlanReminderHandler 计划定时任务
func SundayPlanReminderHandler() error {
	//todo 获取所有用户姓名和用户ID
	// 发送消息
	userMap := make(map[string]string)
	userMap[consts.USERID] = "Batman"
	ctx := context.Background()
	redis_mq.SendSundayPlanRemindMsg(ctx, userMap)
	return nil
}
